const UID = localStorage.getItem('UID')   //获取当前登录ID
const idNumber = document.querySelector('.idNumber')    //学号
const names = document.querySelector('.name')    //姓名

let teacherName

// 获取基本信息
function baseInformation() {
  axios({
    url: 'http://127.0.0.1:8080/Teacher/PersonData/Information',
    params: {
      id: localStorage.getItem('UID')
    }
  }).then(result => {
    // 获取后端结果集
    const resultInformation = result.data.data
    if (!resultInformation) {
      return
    }
    // 填充信息
    idNumber.innerHTML = resultInformation.id
    names.innerHTML = resultInformation.name
  })
}

async function getTeacherName() {
  teacherName = (await axios({
    url: 'http://127.0.0.1:8080/Teacher/CourseManagement/getTeacherNameById',
    params: {
      UID
    }
  })).data.data
}

baseInformation()   // 初始化课程界面
getTeacherName()

async function getAllCourse() {
  const result = (await axios({
    url: 'http://127.0.0.1:8080/Teacher/CourseSchedule/getTeacherCourse',
    params: {
      UID
    }
  })).data.data
  for (let i = 0; i < result.length; i++) {
    const course = result[i]
    // 获取课程在表格的映射
    const box = document.querySelector(`tr[data-id="${course.time}"] td[class="${course.day}"]`)
    // 创建单元格
    const elementBox = createBox(course)
    box.appendChild(elementBox)
  }
}

function createBox(course) {
  const color = ['#FFB142', '#58AFFF', '#0EC6DE', '#87C848']    // 颜色
  const elementBox = document.createElement('div')    //大盒子
  const courseName = document.createElement('div')
  const courseTeacher = document.createElement('div')
  const courseLocation = document.createElement('div')
  const courseInstructionalMethod = document.createElement('div')

  elementBox.style.backgroundColor = color[Math.floor(Math.random() * 3)]   // 随机颜色
  elementBox.classList.add('boxClass')
  elementBox.style.height = (272 * course.constantTime) + 'px'    // 根据节数设置长度
  elementBox.appendChild(courseName)
  elementBox.appendChild(courseTeacher)
  elementBox.appendChild(courseLocation)
  elementBox.appendChild(courseInstructionalMethod)

  courseName.innerHTML = course.name
  courseName.classList.add('courseName')

  courseTeacher.innerHTML = `<i class="iconfont">&#xea7f;</i> ` + teacherName
  courseTeacher.classList.add('courseTeacher')
  courseLocation.innerHTML = `<i class="iconfont">&#xe77f;</i> ` + course.location
  courseInstructionalMethod.innerHTML = `<i class="iconfont">&#xe831;</i> ` + course.instructionalMethod
  return elementBox
}

getAllCourse()